package com.thx.auth.action;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.common.dto.TreeNode;
import com.thx.common.struts2.BaseAction;
import com.thx.common.util.AppContext;
import com.thx.org.model.Company;
import com.thx.org.model.Department;
import com.thx.org.service.CompanyManager;
import com.thx.org.service.DepartmentManager;
import com.thx.role.model.Role;
import com.thx.role.service.RoleManager;
import com.thx.user.model.User;
import com.thx.user.service.UserManager;

@Namespace("/auth")
@Action(value = "orgUserdata", results = {@Result(name = "treeJson", type = "json", params = {
    "root", "tree"})})
@Controller
@Scope("prototype")
public class SelUserAction extends BaseAction {


  private static final long serialVersionUID = 1L;

  public TreeNode tree;

  @Autowired
  protected RoleManager roleMgr;

  @Autowired
  protected DepartmentManager departmentMgr;

  @Autowired
  protected CompanyManager companyMgr;

  @Autowired
  protected UserManager userMgr;

  public SelUserAction() {

  }

  public String roleTree() {
    List<Role> roles =roleMgr.searchAllRoles(null);

    tree = new TreeNode("role-root", "全选", "role", false);

    List<TreeNode> children = new ArrayList<TreeNode>();

    for (Role role : roles) {
      TreeNode node = new TreeNode(role.getRoleId(), role.getRoleName(), "role", true);
      children.add(node);
    }
    tree.setChildren(children);
    return "treeJson";
  }


  public String orgTree() {
    String comId = AppContext.getCurrentComId();
    Company company = companyMgr.get(comId);
    Department root = new Department();
    root.setDepId(comId);
    root.setDepName(company.getComName());
    root.setFullName(company.getComName());
    // --获得该企业的一级部门
    List<Department> depts = departmentMgr.getByParentId(null);
    root.setChildren(depts);
    for (Department d : depts) {
      getChildrenDep(d);
    }
    tree = getTree(root, false, true);
    tree.setVal(false);
    return "treeJson";
  }

  public String userTree() {
    String comId = AppContext.getCurrentComId();
    Company company = companyMgr.get(comId);
    Department root = new Department();
    root.setDepId(comId);
    root.setDepName(company.getComName());
    root.setFullName(company.getComName());
    // --获得该企业的一级部门
    List<Department> depts = departmentMgr.getByParentId(null);
    List<User> users = userMgr.findByDeptId(null);
    if (users != null && users.size() > 0) {
      root.setUsers(new HashSet(users));
    }
    root.setChildren(depts);
    for (Department d : depts) {
      getChildrenDepAndUsers(d);
    }
    tree = getTree(root, true, false);
    return "treeJson";
  }

  protected void getChildrenDep(Department dep) {
    List<Department> children = departmentMgr.getByParentId(dep.getDepId());
    dep.setUsers(null);
    if (children != null && children.size() > 0) {
      dep.setChildren(children);
      for (Department child : children) {
        getChildrenDep(child);
      }
    }
  }

  protected void getChildrenDepAndUsers(Department dep) {

    Set<User> userSet = dep.getUsers();
    if (userSet == null || userSet.size() == 0) {
      List<User> users = userMgr.findByDeptId(dep.getDepId());
      if (users != null && users.size() > 0) {
        dep.setUsers(new HashSet(users));
      }
    }

    List<Department> children = departmentMgr.getByParentId(dep.getDepId());
    if (children != null && children.size() > 0) {
      dep.setChildren(children);
      for (Department child : children) {
        getChildrenDepAndUsers(child);
      }
    }
  }

  protected TreeNode getTree(Department dept, boolean userIsVal, boolean depIsVal) {
    TreeNode tree = new TreeNode(dept.getDepId(), dept.getDepName(), "dep", depIsVal);
    List<TreeNode> childrenOp = new ArrayList<TreeNode>();

    List<Department> children = dept.getChildren();

    if (children != null && children.size() > 0) {
      List<TreeNode> depsOP = new ArrayList<TreeNode>();
      for (Department depTemp : children) {
        TreeNode node = getTree(depTemp, userIsVal, depIsVal);
        depsOP.add(node);
      }
      childrenOp.addAll(depsOP);
    }

    Set<User> users = dept.getUsers();
    if (users != null && users.size() > 0) {
      List<TreeNode> userOp = new ArrayList<TreeNode>();
      for (User user : users) {
        TreeNode op = new TreeNode(user.getUserId(), user.getRealName(), "user", userIsVal);
        userOp.add(op);
      }
      childrenOp.addAll(userOp);
    }

    tree.setChildren(childrenOp);
    return tree;
  };

  public RoleManager getRoleMgr() {
    return roleMgr;
  }

  public void setRoleMgr(RoleManager roleMgr) {
    this.roleMgr = roleMgr;
  }
}
